<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

class admincp{
	public function __construct(){
		$this->checklogin();
		$this->cppath = ADMINCP_PATH;
		$this->actions = $this->actions();
		$this->groups = $this->groups();
		$this->admin['group'] = $this->groups[$this->admin['groupid']];

		$this->founder = $this->admin['founder'];
		$this->manage = $this->admin['group']['rank'] >= 50 ? 1: 0 || $this->founder;
		$this->admin['group']['action'] = $this->founder ? 'all' : $this->admin['group']['action'];
		$this->allow = ($this->admin['group']['action'] == 'all') ? $this->admin['group']['action'] : json_decode($this->admin['group']['action'], true);
		$this->admins = parse_array(limitquery('admincp_member'));
	}

	public function allow($action = '', $operation = ''){
		global $_G;
		//设置菜单
		$_G['admincp']['action'] = $action;
		$_G['admincp']['operation'] = $operation;
		foreach( $this->actions as $k => $v ){
			if ( $this->allow == 'all' || $this->allow[$v['name']] ){
				if ( !$v['submenu'] ){
					continue;
				}
				if ( $v['name'] == $action ){
					$this->title($v['title']);
				}
				if ( $v['display'] ){
					$_G['admincp']['menu'][$v['name']] = $v;
				}
				if ( $v['submenu'] ){
					foreach( $v['submenu'] as $submenu ){
						if ( $submenu['display'] || $submenu['name'] == $operation ){
							if ( $this->allow == 'all' || $this->allow[$v['name']][$submenu['id']] == $submenu['name'] ){
								$this->defopt($v['name'], $submenu['name']);
								$this->addmenu($submenu['name'], $submenu['title'], $submenu['id'], $v['name'], 0);
								if ( $submenu['name'] == $operation && $v['name'] == $action ){
									$this->title($submenu['title'], 'opt');
								}
							}
						}
					}
				}
			}
		}

		if ( !$_G['admincp']['actname'] || !$_G['admincp']['optname'] ){
			return 0;
		}
		if ( $this->allow == 'all' ){
			return $this->allow;
		}

		if ( isset($this->allow[$action]) || $this->allow == 'all' ){
			if ( $operation && in_array($operation, $this->allow[$action]) || !$this->allow[$action] ){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}

	public function checklogin(){
		$nologin = false;
		if ( !$this->adminid = $this->islogin() ){
			$nologin = true;
		}
		if ( !$this->admin = fetch('admincp_member', $this->adminid, 'id', 'admincp_login_' . $this->adminid, 3600)){
			$nologin = true;
		} 
		if ( $nologin ){
			if ( AJAX ){
				jsonp(array(
					'nologin' => 1,
					'data' => render('admin_login_dialog'),
				));
			}
			redirect('login.php?referer=' . HTTP_REFERER);
		}
	}

	public function islogin(){
		return Session::Get('adminid');
	}

	public function ajaxfile($action = '', $operation = ''){
		$ajaxfile = $this->cppath . "/modulus/{$action}/ajax_{$operation}.php";
		if ( is_file($ajaxfile) ){
			return $ajaxfile;
		}
	}

	public function cpfile($action = '', $operation = ''){
		$path = $this->cppath . "/modulus/{$action}/{$operation}.php";
		if ( is_file($path) ){
			return $path;
		}else{
			$bug = new Bug();
			$bug->system("<br />Modulus {$action} doesn't exists.<br />Operation:{$operation}", 0000);
		}
	}

	public function cpajax($action = '', $operation = ''){
		$ajaxfile = $this->cppath . "/modulus/{$action}/{$operation}_ajax.php";
		if ( is_file($ajaxfile) ){
			return $ajaxfile;
		}
	}

	public function save_config($config, $allow = '', $mixed = '', $POST){
		$POST = $POST ? $POST : $_POST;
		$allow = $allow ? explode(',', $allow) : array();
		$mixed = $mixed ? explode(',', $mixed) : array();

		if ( $allow ){
			foreach( $allow as $k => $v ){

			}
		}
		foreach( $POST as $k => $v ){

		}

		$path = SYS_PATH . "configs/system/{$config}.conf.php";
		$data = var_code($POST);
		return file_put_contents($path, $data);
	}

	public function load_config($conf = '', $key = ''){
		return conf($conf, 'admincp', $key);
	}

	public function log($table = '', $dataid = 0, $title = '', $data = array(), $type = 'log'){
		return DB::Insert('system_log', array(
			'adminid' => $this->adminid,
			'table' => $table,
			'dataid' => $dataid,
			'title' => $title,
			'data' => is_array($data) ? json_encode($data) : '',
			'type' => $type,
			'create_time' => SYS_TIME
		));
	}

	public function var_code($data = array()){
		$code = "<?php\n";
		$code .= "if ( !defined('IN_SEC') ){\n\texit();\n}\n\n";
		$data = var_export($data, true);
		$code .= "return {$data}";
		$code .= ";\n\n?>";
		return $code;
	}

	public function actions(){
		$actions = limitquery('admincp_action', array(), array(
			'order' => 'ORDER BY `sort_order` ASC, `id` ASC'
		));
//		delcache('admincp_action');
		return $this->parse_actions($actions);
	}

	public function parse_actions($actions = array(), $parent = 0){
		$admincp_actions = array();
		foreach( $actions as $k => $v ){
			if ( $v['parentid'] == $parent ){
				$admincp_actions[$v['id']] = $v;
				$admincp_actions[$v['id']]['submenu'] = $this->parse_actions($actions, $v['id']);
				if ( empty($admincp_actions[$v['id']]['submenu']) ){
					unset($admincp_actions[$v['id']]['submenu']);
				}
			}
		}
		return $admincp_actions;
	}

	public function groups(){
		$groups = limitcache('admincp_group', 'admincp_group', 0);
		return parse_array($groups);
	}


	public function addmenu($name = '', $title = '', $id = 0, $act = '', $reset = 1){
		global $_G;
		$act = $act ? $act : $_G['gp_action'];
		$id = $id ? $id : rand(100, 999);
		if ( is_array($name) ){
			$submenu = &$name;
			$_G['admincp']['registry'][$act][$submenu['name']] = $submenu['id'];
			$_G['admincp']['submenu'][$act][$submenu['id']] = $submenu;
		}else{
			if ( !$_G['admincp']['submenu'][$act][$id] && !$_G['admincp']['registry'][$act][$name] ){
				$_G['admincp']['registry'][$act][$name] = $id;
				$_G['admincp']['submenu'][$act][$id] = array(
					'name' => $name,
					'title' => $title,
				);
			}
		}
		if ( $reset ){
			$this->title($title);
		}
	}

	public function editmenu($name = '', $title = '', $act = '', $reset = 1){
		global $_G;
		$act = $act ? $act : $_G['gp_action'];
		if ( $id = $_G['admincp']['registry'][$act][$name] ){
			$_G['admincp']['submenu'][$act][$id]['title'] = $title;
			$reset ? $this->title($title, 'opt') : null;
		}
	}

	public function title(&$title = '', $act = 'act'){
		global $_G;
		$_G['admincp'][$act . 'name'] = $title;
	}

	public function defopt($action = '', $operation = ''){
		global $_G;
		if ( !$_G['admincp']['default'][$action] ){
			$_G['admincp']['default'][$action] = $operation;
		}
	}

	public function redirect($action = '', $operation = '', $param = 0, $key = 'id'){
		$querystring = array(
			'action' => $action,
			'operation' => $operation,
		);
		if ( is_array($param) ){
			$querystring = array_merge($querystring, $param);
		}else{
			$param ? $querystring[$key] = $param : null;
		}
		$querystring = http_build_query($querystring);
		// redirect('?' . $querystring);
		redirect($querystring);
	}

	public function noallow(){
		global $_G;
		if ( AJAX ){
			json(array(
				'authorize' => 0,
				'msg' => $msg,
			));
		}else{
			$use_link = '../';
			die(include template('admin_noallow'));
		}
	}

	public function json_type($type = ''){
		$this->json_type = $type;
	}

	public function cpmsg($msg, $link = '', $jump = 1, $error = 1, $json = array()){
		if ( AJAX ){
			jsonp(array(
				'data' => $msg,
				'error' => $error,
				'link' => $link,
				'jump' => $jump,
				'opt' => $json,
			), $this->json_type ? $this->json_type : 'alert');
		}
		global $_G;
		$use_link = '../';
		die(include template('admin_cpmsg'));
	}

	public function jsonp($json = array()){
		$this->jsonp = $json;
	}

	public function success($msg = '', $link = '', $jump = 1, $json = array()){
		$msg = $msg ? $msg : '操作成功';
		$json = $json ? $json : ($this->jsonp ? $this->jsonp : $json);
		return $this->cpmsg($msg, $link, $jump, 0, $json);
	}

}

?>